package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.BuildingService;
import com.api.shopapi.service.InstitutionStaffRelService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 * 楼宇
 */
@RestController
@RequestMapping("Building")
public class BuildingController extends BaseController {

    @Autowired
    @Qualifier("BuildingServiceImplOne")
    private BuildingService buildingService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            Integer floor = jsonObject.getInteger("floor");
            String name = jsonObject.getString("name");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Building/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff = staffService.getByStaffCode(staffCode);
            if(staff == null){
                return failed("职工不存在");
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();


            Building building = buildingService.getByBuildingName(name,institutionCode);
            if(building == null){
                building = new Building();
                building.setBuildingCode(UUID.randomUUID().toString());
                building.setStaffCode(staffCode);
                building.setName(name);
                building.setFloor(floor);
                building.setInstitutionCode(institutionStaffRel.getInstitutionCode());
                buildingService.insert(building);
            }else {
                return failed("楼宇已存在");
            }
            Object data = new Object[]{};
            return success(data);
        }catch (Exception e){
            insertError("/Building/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult UpdateBybuildingCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int floor = jsonObject.getInteger("floor");
            String buildingCode = jsonObject.getString("buildingCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Building/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Building building = buildingService.getByBuildingCode(buildingCode);
            if (building == null) {
                return failed("楼宇不存在");
            } else {
                building.setFloor(floor);
                building.setName(name);
                buildingService.updateByBuildingCode(building);
            }
            Object data = new Object[]{};
            return success(data);
        }catch (Exception e){
            insertError("/Building/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Delete", method = RequestMethod.POST)
    public MyResult DeleteByBuildingCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String buildingCode = jsonObject.getString("buildingCode");
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Building/Delete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Building building = buildingService.getByBuildingCode(buildingCode);
            if (building == null) {
                return failed("楼宇不存在");
            } else {
                buildingService.deleteByBuildingCode(buildingCode);
            }
            Object data = new Object[]{};
            return success(data);
        }catch (Exception e){
            insertError("/Building/Delete", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }



    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult GetListAllByStaffCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Building/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff = staffService.getByStaffCode(staffCode);
            if(staff == null){
                return failed("职工不存在");
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<Building> listBuilding = buildingService.getListAllByInstitutionCode(institutionCode);
            return success(listBuilding);
        }catch (Exception e){
            insertError("/Building/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult getPageList(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Building/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<Building> pageInfoBuilding = buildingService.getPageList(pageIndex,pageSize,institutionCode,name);
            return success(pageInfoBuilding);
        }catch (Exception e){
            insertError("/Building/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Building/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<Building> pageInfoBuilding = buildingService.pageByGovCode(pageIndex,pageSize, staff.getGovernmentCode());

            return success(pageInfoBuilding);
        }catch (Exception e){
            insertError("/Building/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
